/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package deatax.display.miniboard;

import deatax.GameData;
import deatax.Tile;
import deatax.display.TileRenderer;
import javax.media.opengl.GL2;
import javax.media.opengl.fixedfunc.GLMatrixFunc;
import javax.media.opengl.glu.GLU;

/**
 *
 * @author nrechten
 */
public class SelectedTileRenderer {

    GameData data;

    float rotation = 0;

    public SelectedTileRenderer(GameData data) {
        this.data = data;
    }

    public void draw(GL2 gl, GLU glu) {

        Tile selected = data.getMap().getSelectedTile();

//        Tile selected = data.getMap().getTile(50, 50);

        if (selected != null) {
//            System.out.println("Drawing tile");
            int[] viewport = new int[4];
            gl.glGetIntegerv(GL2.GL_VIEWPORT, viewport, 0);

            gl.glViewport(0, 0, MiniBoard.SMALL_WIDTH, MiniBoard.SMALL_HEIGHT);


            gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
            gl.glLoadIdentity();    // reset
            glu.gluPerspective(45.0f, MiniBoard.SMALL_WIDTH/MiniBoard.SMALL_HEIGHT, 0.1f, 500.0f);

            gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
            gl.glLoadIdentity();

//            gl.glDisable(GL2.GL_DEPTH_BUFFER);
            gl.glEnable(GL2.GL_BLEND);
            gl.glDisable(GL2.GL_CULL_FACE);
            glu.gluLookAt(0, -8.5f, 8.5f, 0, 0, 0, 0, 1, 0);


            TileRenderer renderer = data.getRender().findRenderer(selected.getTerrain().getName());
//            System.out.println(renderer.getClass().getName() +  ":" + selected.getTerrain().getName());
            gl.glRotatef(rotation, 0, 0, 1);
            renderer.draw(gl, selected, 0, 0, 0, 0);
            renderer.draw(gl, selected, 0, 0, 0, 1);

            renderer.draw(gl, selected, 0, 0, 0, 2);

            gl.glRotatef(rotation, 0, 0, -1);
            rotation += 0.1f;

            gl.glViewport(viewport[0], viewport[1], viewport[2], viewport[3]);

            gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
            gl.glLoadIdentity();    // reset
            glu.gluPerspective(45.0f, viewport[2] / (float) viewport[3], 0.1f, 500.0f);

            gl.glEnable(GL2.GL_CULL_FACE);
//            gl.glEnable(GL2.GL_DEPTH_BUFFER);

        }
    }
}
